<?php

namespace app\admin\controller;

use app\common\model\SiteConfig;
use app\common\model\Cost as cs;
use think\Controller;
use think\Request;

class Cost extends Controller
{
    /**
     * 默认注册收费规则
     * @return void
     */
    public function index()
    {
        $site = new cs();
        if ($this->request->isPost()) {
            $site_info = $this->request->post();
            if ($site->upinfo($site_info)) {
                return json_x(0, '更新成功');
            }
            return json_x(-1, '更新失败');
        } else {
            return json_x(-1, '请求错误');
        }
    }

    public function main()
    {
        $cost = new cs();
        $cost_info = $cost->getinfo();
        //输出模板元素
        $list = ['cost' => $cost_info];
        return $this->fetch('admin/main/pay_set', $list);
    }

    public function pay_ui()
    {
        $cost = new cs();
        if ($this->request->isPost()) {
            if(DEMO == 1){
                return json_x(3, '演示模式禁止修改');
            }
            $content = $this->request->param();
            if ($cost->upinfo($content)) {
                return json_x(0, '修改成功');
            }
            return json_x(-1, '修改失败');
        } else {
            $info = $cost->getinfo(0);

            return $this->fetch('admin/main/pay_ui', ['info' => $info,'host'=>$this->request->domain()]);
        }
    }
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move('../public/uploads/');
        if ($info) {
            return json(['location' => $info->getSaveName()]);
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
